<template>
  <div>
    <!-- 准备一个dom放置 -->
    <div ref="echart" style="width:400px;height: 400px;" />
  </div>
</template>

<script>
// 导入echarts
import * as echarts from 'echarts'
export default {
  name: 'MyEcharts',
  props: {
    indicator: {
      type: Array,
      // 要求我们传一个默认项-->是需要手写一个工产函数
      default: () => {
        return []
      }
    }
  },
  watch: {
    indicator() {
      // 合并老参数和新参数进行修改操作
      this.myChart.setOption({
        ...this.option,
        radar: {
          indicator: this.indicator
        }
      })
    }
  },

  mounted() {
    // 调用让图表渲染
    this.echartInit()
  },
  methods: {
    echartInit() {
      // 准备一个dom节点用来放置图表 --> ref
      // 渲染到dom --> 在mounted生命周期中
      this.myChart = echarts.init(this.$refs.echart)
      // 配置项
      this.option = {
        legend: {
          data: ['Allocated Budget', 'Actual Spending']
        },
        radar: {
          indicator: this.indicator
        },
        series: [
          {
            name: 'Budget vs spending',
            type: 'radar',
            data: [
              {
                value: [4200, 3000, 20000, 35000, 50000, 18000],
                name: 'Allocated Budget'
              },
              {
                value: [5000, 14000, 28000, 26000, 42000, 21000],
                name: 'Actual Spending'
              }
            ]
          }
        ]
      }
      // 把配置项渲染到实例中
      this.option && this.myChart.setOption(this.option)
    }
  }

}
</script>

<style>

</style>
